package czk.jvm.lecture.bytecode;

import java.lang.reflect.Proxy;
import java.util.Objects;

/**
 * 实现动态代理
 */
public class Client {
    public static void main(String[] args) {
        //设置属性，将生成的字节码文件存储在项目的根文件夹下
        //默认是为null
        //设置为true则会将生成的字节码保存
        System.getProperties().put("sun.misc.ProxyGenerator.saveGeneratedFiles","true");
        Subject subject=new SubjectImpl();
        DynamicSubject dynamicSubject=new DynamicSubject(subject);
        //强转成subject对象
        Subject sub=(Subject) Proxy.newProxyInstance(subject.getClass().getClassLoader(),subject.getClass().getInterfaces(),dynamicSubject);
        sub.response();
        System.out.println(sub.getClass());
        //强转成subject1对象
        Subject1 sub1=(Subject1) Proxy.newProxyInstance(subject.getClass().getClassLoader(),subject.getClass().getInterfaces(),dynamicSubject);
        //Subject1的调用test()方法
        sub1.test();
        System.out.println(sub1.getClass());
        System.out.println(sub.getClass());
        System.out.println(sub.getClass().getSuperclass());
        String property = System.getProperty("sun.misc.ProxyGenerator.saveGeneratedFiles");
        System.out.println(property);
    }
}
